include ../.env

ARIFLOW_VERSION?=2.1.4
NAMESPACE?=uco-etl
AIRFLOW_CHART_VERSION?=8.5.3
PYTHON_VERSION=3.8
GIT_REPO?=gitlab-registry.f.uco.com/p/it/big/uco-prefect
export

.PHONY: deploy reload create_secret docker_reg init install update test uninstall update_helm_repo teardown build

deploy: init create_secret install

reload: clear_secrets create_secret update

create_secret: docker_reg
	kubectl create secret generic \
  		uco-airflow-db-secret \
  		--from-literal=username="app_airflow" \
  		--from-literal=password="${AIRFLOW_DB_PW}" \
  		-n ${NAMESPACE} --dry-run=client -oyaml | kubectl apply -f -
	kubectl create secret generic \
  		uco-airflow-fernet-key \
  		--from-literal=value="${AIRFLOW_FERNET_KEY}" \
  		-n ${NAMESPACE} --dry-run=client -oyaml | kubectl apply -f -
	kubectl create secret generic \
  		uco-airflow-admin-password \
  		--from-literal=value="${AIRFLOW_ADMIN_PW}" \
  		-n ${NAMESPACE} --dry-run=client -oyaml | kubectl apply -f -
	kubectl create secret generic \
  		uco-airflow-git-secret \
  		--from-literal=username="${GITLAB_REPO_USER}" \
		--from-literal=password="${GITLAB_REPO_PASSWORD}" \
  		-n ${NAMESPACE} --dry-run=client -oyaml | kubectl apply -f -
	kubectl create secret generic \
  		uco-airflow-dbt-secret \
  		--from-literal=username="${DBT_USER}" \
		--from-literal=password="${DBT_PASSWORD}" \
		--from-literal=webhook="${DBT_WEBHOOK}" \
		--from-literal=mrs_webhook="${DBT_MRS_WEBHOOK}" \
  		-n ${NAMESPACE} --dry-run=client -oyaml | kubectl apply -f -
	kubectl create secret generic \
  		uco-airflow-dbt-kyuubi-secret \
  		--from-literal=username="${DBT_MRS_USER}" \
		--from-literal=password="${DBT_MRS_PASSWORD}" \
  		-n ${NAMESPACE} --dry-run=client -oyaml | kubectl apply -f -
	kubectl create secret generic \
		uco-airflow-common-secret \
		--from-literal=webhook=${AIRFLOW_WEBHOOK} \
		--from-literal=integration_webhook=${AIRFLOW_INTEGRATION_WEBHOOK} \
		-n ${NAMESPACE} --dry-run=client -oyaml | kubectl apply -f -
# --from-file=krb5.conf=./resources/krb5.conf 
# --from-file=airflow.keytab=./resources/airflow.keytab

# .PHONY: git_ssh
# git_ssh:
# 	kubectl create secret generic \
#   		uco-airflow-git-secret \
#   		--from-file=id_rsa=.id_rsa \
#   		-n ${NAMESPACE}

docker_reg:
	kubectl create secret docker-registry \
		uco-gitlab \
    	--docker-server=gitlab-registry.f.uco.com \
    	--docker-username=${GITLAB_DOCKER_USER} \
    	--docker-password="${GITLAB_DOCKER_PASSWORD}" \
  		-n ${NAMESPACE} --dry-run=client -oyaml | kubectl apply -f -

init:
	kubectl create namespace ${NAMESPACE}

install:
	envsubst < airflow-value.yaml > build/airflow-value.yaml
	helm upgrade --install -f build/airflow-value.yaml uco-airflow airflow-stable/airflow \
	-n ${NAMESPACE} \
	--set airflow.users[0].password="${AIRFLOW_ADMIN_PW}" \
	--set airflow.config.AIRFLOW__WEBSERVER__SECRET_KEY="${AIRFLOW_WEBSERVER_SECRET}" \
	--set airflow.image.repository=${GIT_REPO}/airflow \
	--set airflow.image.tag=${ARIFLOW_VERSION}-python${PYTHON_VERSION} \
	--set-file web.webserverConfig.stringOverride=ldap_config.py \
	--version ${AIRFLOW_CHART_VERSION} \
	--debug

test:
	envsubst < airflow-value.yaml > build/airflow-value.yaml
	helm template -f airflow-value.yaml uco-airflow airflow-stable/airflow \
	-n ${NAMESPACE} \
	--set users[0].password="${AIRFLOW_ADMIN_PW}" \
	--set airflow.config.AIRFLOW__WEBSERVER__SECRET_KEY="${AIRFLOW_WEBSERVER_SECRET}" \
	--set airflow.image.repository=${GIT_REPO}/airflow \
	--set airflow.image.tag=${ARIFLOW_VERSION}-python${PYTHON_VERSION} \
	--set-file web.webserverConfig.stringOverride=ldap_config.py \
	--version ${AIRFLOW_CHART_VERSION} \
	--debug > build/uco-airflow_full_${AIRFLOW_CHART_VERSION}.yaml

monitors:
	kubectl apply -f prometheus-monitor.yaml -n ${NAMESPACE}
	kubectl apply -f prometheus-rules.yaml -n ${NAMESPACE}

uninstall:
	-helm uninstall uco-airflow -n ${NAMESPACE}

teardown: clear_secrets uninstall
	kubectl delete -f prometheus-monitor.yaml -n ${NAMESPACE}
# -kubectl delete --all pods -n ${NAMESPACE}
# kubectl delete PersistentVolumeClaim local-pvc
# kubectl delete -f local-volume-provisioner.generated.yaml
# -kubectl delete namespace ${NAMESPACE}

# helm_local_storage:
# 	git clone https://github.com/kubernetes-sigs/sig-storage-local-static-provisioner.git
# 	helm template -f custom-storage-provision-value.yaml uco-airflow-storage -n ${NAMESPACE} ./sig-storage-local-static-provisioner/helm/provisioner > local-volume-provisioner.generated.yaml
# 	helm install -f custom-storage-provision-value.yaml uco-airflow-storage -n ${NAMESPACE} ./sig-storage-local-static-provisioner/helm/provisioner

# helm template -f build/airflow-value.yaml uco-airflow-storage -n ${NAMESPACE} --version 8.5.2 airflow-stable/airflow > test.yaml

build:
	docker build --build-arg ARIFLOW_VERSION=${ARIFLOW_VERSION} \
		-t ${GIT_REPO}/airflow:${ARIFLOW_VERSION}-python${PYTHON_VERSION} .
	docker push ${GIT_REPO}/airflow:${ARIFLOW_VERSION}-python${PYTHON_VERSION}